package com.zx.chatroom.util;

import com.zx.chatroom.vo.Chat;
import com.zx.framework.core.util.DateTimeUtil;
import com.zx.framework.core.util.FileUtil;
import org.apache.log4j.Logger;

import java.io.*;
import java.util.Date;

public class LogUtil {

	private static final Logger LOGGER = Logger.getLogger(LogUtil.class);
	/**
	 * 记录保存
	 * @author shizhenhua
	 * @param diskpath 文件输出路径
	 * @param filename 文件名
	 * @param content 文件内容
	 * */
	synchronized public static void writeFile(final String diskpath,final String filename,final String content) throws IOException {
		FileOutputStream fileOutputStream = null;
		OutputStreamWriter outputStreamWriter = null;
		BufferedWriter out = null;
		try {
			fileOutputStream = new FileOutputStream(diskpath+filename,true);
			outputStreamWriter = new OutputStreamWriter(fileOutputStream,"UTF-8");
			out = new BufferedWriter(outputStreamWriter);
			out.write(content);
		} catch (Exception e) {
			LOGGER.error("保存聊天记录失败："+e.getMessage());
		} finally {
			if (null != out) {
				try {
					out.close();
				} catch (Exception e2) {}
			}
			if (null != outputStreamWriter) {
				try {
					outputStreamWriter.close();
				} catch (Exception e2) {}
			}
			if (null != fileOutputStream) {
				try {
					fileOutputStream.close();
				} catch (Exception e2) {}
			}
		}
	}
	/**
	 * log聊天记录
	 * @author shizhenhua
	 * @param charInfo
	 * @param toUser
	 * @param fromUser
	 */
	public static void logChatInfo(String path,String charInfo,String toUser,String toUserName,String fromUser,String fromUserName){
		try {
			Date d = new Date();
			//记录时间
			String date = DateTimeUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
			StringBuilder content = new StringBuilder(date+"	"+fromUserName+" TO "+toUserName+"\r\n");
			content.append(charInfo+"\r\n");
			String strdate = DateTimeUtil.dateToString(d, "yyyy-MM-dd");
			//文件输出路径
			path = path + strdate;
			File basePath = new File(path);
			if (!basePath.isDirectory()) {
				basePath.mkdirs();
			}
			File file = new File(basePath+File.separator+strdate+"("+toUser+" TO "+fromUser+").txt");
			if (file.isFile()) {
				
			} else if ((file = new File(basePath+File.separator+strdate+"("+fromUser+" TO "+toUser+").txt")).isFile()) {
				
			}
			FileUtil.writeStringToFile(file, content.toString(), "UTF-8",true);
		} catch (Exception e) {
			LOGGER.error("聊天记录封装失败："+e.getMessage());
		}
	}
	
	/**
	 * talklog聊天记录
	 * @author shizhenhua
	 * @param path
	 * @param charInfo
	 */
	synchronized public static void talKLogChatInfo(String path,Chat charInfo){
		try {
			Date d = new Date();
			//记录时间
			String date = DateTimeUtil.dateToString(d, "yyyy-MM-dd HH:mm:ss");
			StringBuilder content = new StringBuilder(date+"	"+charInfo.getFromUserName()+"\r\n");
			content.append(charInfo.getMessage()+"\r\n");
			String strdate = DateTimeUtil.dateToString(d, "yyyy-MM-dd");
			//文件输出路径
			path = path + strdate;
			File basePath = new File(path);
			if (!basePath.isDirectory()) {
				basePath.mkdirs();
			}
			File file = new File(basePath+File.separator+strdate+"("+charInfo.getGroupId()+").txt");
			if (file.isFile()) {
				
			}
			FileUtil.writeStringToFile(file, content.toString(), "UTF-8",true);
		} catch (Exception e) {
			LOGGER.error("聊天记录封装失败："+e.getMessage());
		}
	}
	
}
